package com.hp.controller;

import com.hp.pojo.Category;
import com.hp.service.CategoryService;
import com.hp.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/show")
    public String tbCategoryIndex(){
        return "tbCategory";
    }

    @RequestMapping("/findList")
    @ResponseBody
    public List<Category> findList(int parentId){

        return categoryService.findList(parentId);
    }
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Category> findAll(){

        return categoryService.findAll();
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Category category, HttpSession session){
        System.out.println(category);
        try {
           categoryService.add(category);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody Category category){
        System.out.println(category);
        try {
            categoryService.update(category);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @GetMapping("/findById")
    @ResponseBody
    public Category findById(int id){
        Category byId = categoryService.findById(id);
        return byId;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Result delete(int id){
        try {
            categoryService.delete(id);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }

}
